package com.connection.second.wallpaper.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.connection.second.common.annotation.Anonymous;
import com.connection.second.wallpaper.domain.entity.PaperAlbum;
import com.connection.second.wallpaper.domain.entity.PaperWall;
import com.connection.second.wallpaper.domain.vo.PaperAlbumVo;
import com.connection.second.wallpaper.domain.vo.PaperWallVo;
import com.connection.second.wallpaper.domain.vo.UserPostingStatistics;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.connection.second.common.annotation.Log;
import com.connection.second.common.core.controller.BaseController;
import com.connection.second.common.core.domain.AjaxResult;
import com.connection.second.common.enums.BusinessType;
import com.connection.second.wallpaper.domain.entity.PaperUserPosting;
import com.connection.second.wallpaper.service.IPaperUserPostingService;
import com.connection.second.common.utils.poi.ExcelUtil;
import com.connection.second.common.core.page.TableDataInfo;

/**
 * 发布审核Controller
 * 
 * @author FL
 * @date 2024-11-15
 */
@Api(tags = "发布审核管理")
@RestController
@RequestMapping("/wallpaper/posting")
@Slf4j
public class PaperUserPostingController extends BaseController
{
    @Autowired
    private IPaperUserPostingService paperUserPostingService;

    /**
     * 查询发布审核列表
     */
    @ApiOperation("查询发布审核列表")
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping("/list")
    public TableDataInfo list(PaperUserPosting paperUserPosting)
    {
        startPage();
        List<PaperUserPosting> list = paperUserPostingService.selectPaperUserPostingList(paperUserPosting);
        return getDataTable(list);
    }

    /**
     * 用户查看自己发布的内容（壁纸或专辑）
     */
    @ApiOperation("用户查看自己发布的内容（壁纸或专辑）")
    @PreAuthorize("@ss.hasRole('user')")
    @GetMapping("/userPosts")
    public TableDataInfo userPosts(PaperUserPosting paperUserPosting) {
        startPage();
        Integer type = paperUserPosting.getType();
        if (type == 2) {
            // 2表示获取专辑
            List<PaperAlbumVo> list = paperUserPostingService.selectUserAlbumList(paperUserPosting);
            return getDataTable(list);
        } else if (type == 1) {
            // 1表示获取壁纸
            List<PaperWallVo> list = paperUserPostingService.selectUserWallpaperList(paperUserPosting);
            return getDataTable(list);
        } else {
            // 如果type参数不合法，可以抛出异常或返回错误信息
            throw new IllegalArgumentException("Invalid type parameter: must be 0 (album) or 1 (wallpaper)");
        }
    }

    /**
     * 用户查看自己发布的内容（壁纸或专辑）
     */
    @ApiOperation("用户查看自己发布的内容详情（壁纸或专辑）")
    @PreAuthorize("@ss.hasRole('user')")
    @GetMapping("/postDetail")
    public AjaxResult userPostDetail(PaperUserPosting paperUserPosting) {
        Integer type = paperUserPosting.getType();
        if (type == 2) {
            // 2表示获取专辑
            PaperAlbumVo album = paperUserPostingService.selectAlbumById(paperUserPosting.getId());
            return AjaxResult.success(album);
        } else if (type == 1) {
            // 1表示获取壁纸
            PaperWallVo wall = paperUserPostingService.selectWallpaperById(paperUserPosting.getId());
            return AjaxResult.success(wall);
        } else {
            // 如果type参数不合法，可以抛出异常或返回错误信息
            return AjaxResult.error("参数错误：type参数必须为1或2");
        }
    }


    /**
     * 用户发布壁纸
     */
    @ApiOperation("用户发布壁纸")
    @PreAuthorize("@ss.hasAnyRoles('user,admin')")
    @Log(title = "用户发布壁纸", businessType = BusinessType.INSERT)
    @PostMapping("/wallpaper")
    public AjaxResult userPostWallpaper(@RequestBody PaperWall paperWall)
    {
        return toAjax(paperUserPostingService.userPostWallpaper(paperWall));
    }

    /**
     * 用户发布专辑
     */
    @ApiOperation("用户发布专辑")
    @PreAuthorize("@ss.hasAnyRoles('user,admin')")
    @Log(title = "用户发布专辑", businessType = BusinessType.INSERT)
    @PostMapping("/album")
    public AjaxResult userPostAlbum(@RequestBody PaperAlbum paperAlbum)
    {
        return toAjax(paperUserPostingService.userPostAlbum(paperAlbum));
    }

    /**
     * 用户取消发布
     */
    @PreAuthorize("@ss.hasRole('user')")
    @Log(title = "发布审核", businessType = BusinessType.UPDATE)
    @PutMapping("/user")
    public AjaxResult editByUser(@RequestBody PaperUserPosting paperUserPosting)
    {
        log.info("用户取消发布：{}", paperUserPosting);
        PaperUserPosting tmp = new PaperUserPosting();
        tmp.setType(paperUserPosting.getType());
        tmp.setItemId(paperUserPosting.getItemId());
        tmp.setUserId(paperUserPosting.getUserId());
        List<Long> ids = paperUserPostingService.selectPaperUserPostingList(tmp)
                        .stream()
                        .map(PaperUserPosting::getId)
                        .collect(Collectors.toList());
                for (Long id : ids) {
                    paperUserPostingService.deletePaperUserPostingById(id);
                }

        return AjaxResult.success();

    }

    @ApiOperation("用户发布统计")
    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @PostMapping("/statistics")
    public AjaxResult userPostStatistics()
    {
        List<UserPostingStatistics> list = paperUserPostingService.getUserPostStatistics();
        return AjaxResult.success(list);
    }

    /**
     * 获取发布审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('wallpaper:posting:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(paperUserPostingService.selectPaperUserPostingById(id));
    }

    /**
     * 新增发布审核
     */
    @PreAuthorize("@ss.hasPermi('wallpaper:posting:add')")
    @Log(title = "发布审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PaperUserPosting paperUserPosting)
    {
        return toAjax(paperUserPostingService.insertPaperUserPosting(paperUserPosting));
    }

    /**
     * 修改发布审核
     */
    @PreAuthorize("@ss.hasPermi('wallpaper:posting:edit')")
    @Log(title = "发布审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PaperUserPosting paperUserPosting)
    {
        return toAjax(paperUserPostingService.updatePaperUserPosting(paperUserPosting));
    }

    /**
     * 删除发布审核
     */
    @PreAuthorize("@ss.hasPermi('wallpaper:posting:remove')")
    @Log(title = "发布审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(paperUserPostingService.deletePaperUserPostingByIds(ids));
    }
}
